#include "Converter.h"
   
void GraphStructureOne::AddNode(Znode* b,int i){
	int TypeEnum=b->type();
	Agraph.addNode(TypeEnum,b->data,i);
	NodeToIndex[b]=i;
}
void GraphStructureOne::AddEdge(Znode* b,int i){	
	ProgEdgeIter it = b->edges.begin();			
	for (; it != b->edges.end(); it++){				
	Znode* realsource=&((*it)->from);
	Znode* realdest=&((*it)->to);
	string EdgeWeight=((*it)->weight);
	int SourceFake= NodeToIndex[realsource];
	int DestFake  = NodeToIndex[realdest];
	Agraph.addEdge(SourceFake,DestFake,realsource->data,realdest->data,EdgeWeight,i);
	}
}
void GraphStructureOne::ConvertGraphStructure(GraphStructTwo & b){
	for(int i=0;i<b.vertices.size();i++){	
		AddNode(b.vertices[i],i);			
	}		
	for(int i=0;i<b.vertices.size();i++){				
		AddEdge(b.vertices[i],i);
	}
}
			
void GraphStructureTwo::AddNode(NODE node,int i){
	if (node.type=="Statement"){
		Stmt *nod=new Stmt(node.data);
		StructureTwoNodes.push_back(nod);
		NodeIndex[node.Fake]=i;	
		Bgraph.addNode(nod);
	}
	else if (node.type=="Expression"){
		Expr *nod=new Expr(node.data);
		NodeIndex[node.Fake]=i;
		StructureTwoNodes.push_back(nod);
		Bgraph.addNode(nod);
	}
	else if (node.type=="Literal"){
		Lit *nod=new Lit(node.data,node.data);
		NodeIndex[node.Fake]=i;
		StructureTwoNodes.push_back(nod);
		Bgraph.addNode(nod);
	}
	else{
		Var *nod=new Var(node.data,node.type);
		NodeIndex[node.Fake]=i;
		StructureTwoNodes.push_back(nod);
		Bgraph.addNode(nod);
	}
}
void GraphStructureTwo::AddEdge(EDGE edge){
	Bgraph.addEdge(StructureTwoNodes[NodeIndex[edge.Fakesource]],StructureTwoNodes[NodeIndex[edge.FakeDest]],edge.weight);			
}
void GraphStructureTwo::ConvertGraphStructure(GraphStructOne & b){
	for (int i=0;i<b.Nodes.size();i++){		
		AddNode(b.Nodes[i],i);
	}
	for (int i=0;i<b.Edges.size();i++){
		AddEdge(b.Edges[i]);
	}	
}

